package com.ruoyi.safe.controller.api;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.safe.domain.BizOverseer;
import com.ruoyi.safe.domain.BizProjectCheckRecord;
import com.ruoyi.safe.domain.dto.BizExtensionRequestsDTO;
import com.ruoyi.safe.domain.dto.BizProjectCheckRecordDTO;
import com.ruoyi.safe.domain.dto.BizRectifyReviewDTO;
import com.ruoyi.safe.domain.vo.BizOverseerVo;
import com.ruoyi.safe.domain.vo.BizProjectCheckRecordVO;
import com.ruoyi.safe.domain.vo.ListRequestVo;
import com.ruoyi.safe.service.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 手机端-排除详情接口(张玉玺)
 */
@Slf4j
@RestController
@RequestMapping("/api/excludeDetail")
@RequiredArgsConstructor
public class ExcludeDetailController {

    private final IBizExcludePlanTaskService bizExcludePlanTaskService;

    private final IBizProjectCheckRecordService bizProjectCheckRecordService;

    private final IBizRectifyReviewService bizRectifyReviewService;

    private final IBizExtensionRequestsService bizExtensionRequestsService;

    @Autowired
    private IBizOverseerService bizOverseerService;

    /**
     * 获取隐患排查待办
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ListRequestVo listRequestVo) {
        return AjaxResult.success(bizExcludePlanTaskService.selectBizExcludePlanTaskListByListRequestVo(listRequestVo));
    }

    /**
     * 新增隐患信息和排查记录
     */
    @PostMapping("/handle")
    public AjaxResult handle(@RequestBody @Valid BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {
        bizProjectCheckRecordService.handle(bizProjectCheckRecordDTO);
        return AjaxResult.success();
    }

    /**
     * 根据状态获取隐患排查列表
     */
    @PostMapping("/getRectify")
    public R<Object> getRectify(@RequestBody ListRequestVo listRequestVo) {
        PageInfo<BizProjectCheckRecord> rectify = bizProjectCheckRecordService.getRectify(listRequestVo);
        return R.ok(rectify);
    }

    /**
     * 新增整改记录
     */
    @PostMapping("/addRectify")
    public AjaxResult addRectify(@RequestBody BizRectifyReviewDTO bizRectifyReviewDTO) {
        bizRectifyReviewService.addRectifyReview(bizRectifyReviewDTO);
        return AjaxResult.success();
    }

    /**
     * 修改排查记录表
     */
    @PostMapping("/updateRectify")
    public AjaxResult updateRectify(@RequestBody BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {
        bizProjectCheckRecordService.updateProjectCheckRecord(bizProjectCheckRecordDTO);
        return AjaxResult.success();
    }

    /**
     * 发起督查督办
     */
    @PostMapping("/addBizOverseer")
    public AjaxResult addBizOverseer(@RequestBody BizOverseer bizOverseer) {
        bizOverseer.setStatus("0");
        bizOverseer.setInitOperation("发起督办");
        int i = bizOverseerService.insertBizOverseer(bizOverseer);
        if (i > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("发起督查督办失败");
        }
    }

    /**
     * 处理督查督办
     */
    @PostMapping("/dealBizOverseer")
    public AjaxResult dealBizOverseer(@RequestBody BizOverseer bizOverseer) {
        bizOverseer.setDealOperation("处理督办");
        bizOverseer.setDealTime(DateUtils.getNowDate());
        bizOverseer.setStatus("1");
        int i = bizOverseerService.updateBizOverseer(bizOverseer);
        if (i > 0) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("处理督查督办失败");
        }
    }

    /**
     * 根据隐患id获取隐患信息或排查记录
     */
    @PostMapping("/getRectifyById")
    public R<BizProjectCheckRecordVO> getRectifyByRecordId(@RequestBody ListRequestVo listRequestVo) {
        BizProjectCheckRecordVO vo = bizProjectCheckRecordService.getRectifyByRecordId(listRequestVo);
        BizProjectCheckRecord bizProjectCheckRecord = bizProjectCheckRecordService.selectBizRecordById(vo.getId());
        List<BizOverseerVo> overseerVoList = bizOverseerService.selectOverseerVo(listRequestVo.getRecordId());
        vo.setBizOverseerVo(overseerVoList);
        if (vo.getHiddenDangerStatus().equals("3") || vo.getHiddenDangerStatus().equals("4") || vo.getHiddenDangerStatus().equals("5")) {
            vo.setOverseerFlag(false);
        } else {
            long count = overseerVoList.stream().filter(o -> o.getInitUser().equals(listRequestVo.getUserName())).count();
            if (count > 0) {
                vo.setOverseerFlag(false);
            } else {
                if (listRequestVo.getUserName().equals(bizProjectCheckRecord.getRevisePeople())) {
                    vo.setOverseerFlag(false);
                } else {
                    vo.setOverseerFlag(true);
                }
            }
        }
        return R.ok(vo);
    }

    /**
     * 查询待复查数据
     */
    @PostMapping("/getReview")
    public AjaxResult getReview(@RequestBody ListRequestVo listRequestVo) {
        PageInfo<BizProjectCheckRecord> review = bizProjectCheckRecordService.getReview(listRequestVo);
        return AjaxResult.success(review);
    }

    /**
     * 查询需要批复的延期申请
     */
    @PostMapping("/getDelay")
    public R<Object> getDelay(@RequestBody ListRequestVo listRequestVo) {

        if (listRequestVo.getRecordId() != null) {
            return R.ok(bizExtensionRequestsService.getDelay(listRequestVo));
        }
        return R.ok(new PageInfo<>(bizExtensionRequestsService.getDelay(listRequestVo)));
    }

    /**
     * 审批延期
     */
    @PostMapping("/handleDelay")
    public AjaxResult handleDelay(@RequestBody BizExtensionRequestsDTO bizExtensionRequestsDTO) {
        bizExtensionRequestsService.handleDelay(bizExtensionRequestsDTO);
        return AjaxResult.success();
    }

    /**
     * 根据recordId新增通知人
     */
    @PostMapping("/transfer")
    public AjaxResult transfer(@RequestBody BizProjectCheckRecordDTO bizProjectCheckRecordDTO) {
        bizProjectCheckRecordService.updateNoticePeopleById(bizProjectCheckRecordDTO);
        return AjaxResult.success();
    }

    /**
     * 延期申请
     */
    @PostMapping("/addDelay")
    public AjaxResult addDelay(@RequestBody BizExtensionRequestsDTO extensionRequestsDTO) {
        bizExtensionRequestsService.insertBizExtensionRequests(extensionRequestsDTO);
        return AjaxResult.success();
    }

}
